/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.site.ipodium.action;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import net.iwsh.core.model.bean.NameValueBean;
import net.iwsh.core.model.bean.impl.NameValuePair;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.us.usws.biz.DefaultResourceProvider;
import com.us.usws.biz.ResourceURIReplacer;
import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.base.BaseDict;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.flat.system.DOUserAttr;
import com.us.usws.service.cud.UserCUDService;
import com.us.usws.service.ro.UserROService;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.SiteWebContext;

/**
 * Struts2 action
 * @author robot
 */
public class EditProfileAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	//private static Log log = LogFactory.getFactory().getInstance(EditProfileAction.class);

	@Inject
	UserROService roService;

	@Inject
	UserCUDService cudService;

	private List<NameValueBean> genderTypes = new ArrayList<NameValueBean>(2);

	private String genderType;
	private String emailAddr;
	private String mobileNo;
	private String contactAddr;
	private String qqNo;

	private String avatar;

	/**
	 * constructor to initial gender types
	 */
	public EditProfileAction() {
		List<BaseDict> dicts = CacheManager.getDictCache().getDicts(SiteConstants.DICT_DOMAIN_GENDER_TYPE);
		for (BaseDict each : dicts) {
			genderTypes.add(new NameValuePair(each.getCode(), each.getName()));
		}
	}

	/**
	 * load profile
	 * @return
	 */
	public String load() {
		SiteWebContext swc = new SiteWebContext(ServletActionContext.getRequest(), ServletActionContext.getResponse());
		User u = roService.getUser(swc.getIpodiumUser().getUid());
		this.emailAddr = u.getEmailAddr();
		this.genderType = u.getGenderType();

		if (StringUtils.isBlank(u.getAvatar())) {
			this.avatar = DefaultResourceProvider.getDefaultTeacherAvatar();
		} else {
			this.avatar = ResourceURIReplacer.getUserAvatar(u.getUid(), u.getAvatar());
		}

		if (u.getUserAttr() != null) {
			this.qqNo = u.getUserAttr().getQqNo();
			this.contactAddr = u.getUserAttr().getContactAddr();
			this.mobileNo = u.getUserAttr().getMobileNo();
		}
		return SUCCESS;
	}

	/**
	 * save profile
	 * @return
	 */
	public String save() {
		SiteWebContext swc = new SiteWebContext(ServletActionContext.getRequest(), ServletActionContext.getResponse());
		User u = roService.getUser(swc.getIpodiumUser().getUid());
		u.setEmailAddr(emailAddr);
		u.setGenderType(genderType);
		if (u.getUserAttr() == null) {
			u.setUserAttr(new DOUserAttr());
			u.getUserAttr().setUid(u.getUid());
		}
		u.getUserAttr().setContactAddr(contactAddr);
		u.getUserAttr().setMobileNo(mobileNo);
		u.getUserAttr().setQqNo(qqNo);

		cudService.update(u);

		if (StringUtils.isBlank(u.getAvatar())) {
			this.avatar = DefaultResourceProvider.getDefaultTeacherAvatar();
		} else {
			this.avatar = ResourceURIReplacer.getUserAvatar(u.getAvatar(), u.getAvatar());
		}

		return SUCCESS;
	}

	/**
	 * upload avatar
	 * @return
	 */
	public String uploadAvatar() {
		return SUCCESS;
	}

	/**
	 * validate save
	 */
	public void validateSave() {

	}

	public String getGenderType() {
		return genderType;
	}

	public void setGenderType(String genderType) {
		this.genderType = genderType;
	}

	public String getEmailAddr() {
		return emailAddr;
	}

	public void setEmailAddr(String emailAddr) {
		this.emailAddr = emailAddr;
	}

	public String getMobileNo() {
		return mobileNo;
	}

	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}

	public String getContactAddr() {
		return contactAddr;
	}

	public void setContactAddr(String contactAddr) {
		this.contactAddr = contactAddr;
	}

	public String getQqNo() {
		return qqNo;
	}

	public void setQqNo(String qqNo) {
		this.qqNo = qqNo;
	}

	public List<NameValueBean> getGenderTypes() {
		return genderTypes;
	}

	public void setGenderTypes(List<NameValueBean> genderTypes) {
		this.genderTypes = genderTypes;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

}